﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using log4net;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;

using SysTool._00_Def;

namespace SysTool._02_Service
{
    public class AppointmentService
    {
        private static ILog _logger = LogManager.GetLogger(typeof(AppointmentService));

        public static IList<UserInfoAndReferenceUserInfo> QueryTeamNotAppointmentUsers(int userId, string date)
        {
            var ls = UserService.QueryAllUsersWithSameShopByUserId(userId);
            var userIds = ls.Select(x => x.UserInfo.Id).ToList();
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls2 = db.Queryable<AppointmentEntity>().
                    Where(x => x.Time.ToString().Substring(0, 10) == date && userIds.Contains(x.UserId)).ToList();
                var ls3 = ls.Where(x => !ls2.Exists(y => y.UserId == x.UserInfo.Id)).ToList();

                return ls3;
            }
        }

        public static IList<UserInfoAndReferenceUserInfo> QueryTeamNotAppointmentUsers(string date)
        {
            return QueryTeamNotAppointmentUsers(UserService.LoginUserInfo.Id, date);
        }

        public static IList<UserInfoAndReferenceUserInfo> QueryTeamAppointmentUsers(int userId, string date)
        {
            var ls = UserService.QueryAllUsersWithSameShopByUserId(userId);
            var userIds = ls.Select(x => x.UserInfo.Id).ToList();
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls2 = db.Queryable<AppointmentEntity>().
                    Where(x => x.Time.ToString().Substring(0, 10) == date && userIds.Contains(x.UserId)).ToList();
                var ls3 = ls.Where(x => ls2.Exists(y => y.UserId == x.UserInfo.Id)).ToList();

                return ls3;
            }
        }

        public static IList<UserInfoAndReferenceUserInfo> QueryTeamAppointmentUsers(string date)
        {
            return QueryTeamAppointmentUsers(UserService.LoginUserInfo.Id, date);
        }

        public static IList<AppointmentUserInfoView> QueryShopAppointmentUsers(string date)
        {
            var ls = UserService.QueryAllUsersWithSameShopByUserId(UserService.LoginUserInfo.Id);
            var userIds = ls.Select(x => x.UserInfo.Id).ToList();
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls2 = db.Queryable<AppointmentEntity>()
                    .InnerJoin<UserInfoEntity>((a, u1) => a.UserId == u1.Id)
                    .InnerJoin<UserInfoEntity>((a, u1, u2) => u1.ReferenceInvitationCode == u2.InvitationCode)
                    .Where((a, u1, u2) => a.Time.ToString().Substring(0, 10) == date && userIds.Contains(a.UserId))
                    .Select((a, u1, u2) => new AppointmentUserInfoView
                    {
                        AppointmentInfo = a,
                        UserInfo = u1,
                        ReferenceUserInfo = u2
                    })
                    .ToList();

                var ls3 = ls2.Where(x => ls.Where(y => y.UserInfo.Id == x.AppointmentInfo.UserId).Count() > 0).ToList();
                return ls3;
            }
        }

        public static bool Add(AppointmentEntity entity)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Insertable<AppointmentEntity>(entity).ExecuteCommand();
                return n > 0;
            }
        }
    }
}
